package com.summer.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.summer.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 第三方发送短信验证码
 */
@Slf4j
@Component("youxinSMSConfig")
public class YouxinSMSConfig {
	private static final String URL = "http://www.youxinyun.com:3070/Platform_Http_Service/servlet/SendSms";
	private static final String USERNAME = "shyx";
	private static final String PASSWORD = "523243";

	private Logger logger = LoggerFactory.getLogger(YouxinSMSConfig.class);

	public Boolean sendCode(String mobile,String code,String title) {
		String content = "【${title}】验证码：${code}，30分钟内有效，打死不要告诉别人哦！";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("UserName", USERNAME);
		params.put("Key", getKey(USERNAME, PASSWORD, getTimestemp()));
		params.put("Timestemp", getTimestemp());
		params.put("Content", content.replace("${code}", code).replace("${title}", title));
		params.put("CharSet", "utf-8");
		params.put("Mobiles", mobile);
		params.put("SMSID", code);
		logger.info("友信短信发送参数："+params);
		String result = HttpUtil.doPost(URL, params);
		logger.info("友信短信发送返回结果："+result);
		JSONObject parameters = JSON.parseObject(result);
		Integer i = parameters.getInteger("result");
		if (i == 0) {
			return true;
		}else {
			logger.info("友信短信发送失败");
			return false;
		}
	}

	public Boolean sendWarn(String mobile, String content) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("UserName", USERNAME);
		params.put("Key", getKey(USERNAME, PASSWORD, getTimestemp()));
		params.put("Timestemp", getTimestemp());
		params.put("Content", content);
		params.put("CharSet", "utf-8");
		params.put("Mobiles", mobile);
//		params.put("SMSID", code);
		logger.info("友信短信发送参数：" + params);
		String result = HttpUtil.doPost(URL, params);
		logger.info("友信短信发送返回结果：" + result);
		JSONObject parameters = JSON.parseObject(result);
		Integer i = parameters.getInteger("result");
		if (i == 0) {
			return true;
		} else {
			logger.info("友信短信发送失败");
			return false;
		}
	}

	private static String getKey(String userName, String password, String timestemp) {
		String key = "";
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(userName.getBytes());
			mdTemp.update(password.getBytes());
			mdTemp.update(timestemp.getBytes());
			key = bytesToHexString(mdTemp.digest());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return key;
	}

	private static String getTimestemp() {
		return (new SimpleDateFormat("MMddHHmmss")).format(new Date());
	}

	private static String bytesToHexString(byte[] src) {
		String resultString = "";
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		resultString = stringBuilder.toString();
		stringBuilder = null;
		return resultString;
	}

}
